package es.ats.codington.web.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.services.VisitorService;

/**
 * Servlet implementation class UpdatePassServlet
 */
@WebServlet("/UpdatePassServlet")
public class UpdatePassServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdatePassServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		response.setContentType("text/html");  
	    PrintWriter out = response.getWriter();
	    String n=request.getParameter("username");  
	    String op=request.getParameter("oldpassword");  
	    String np=request.getParameter("newpassword");
		    
	    Visitor u=new Visitor();
	    u.setUsername(n);
	    u.setPassword(op);
	    
	    VisitorService vs=new VisitorService();
	   
	    Object iD = request.getSession().getAttribute("iDVisitor");
	    int id= (Integer) iD;

	    
	    try{
		    boolean tt =vs.updatePassword(u,np,id);
		    if (tt==true){	
		    RequestDispatcher rd=request.getRequestDispatcher("mainpage2.jsp");  
		    rd.forward(request,response);
		    	
		    }
		    
		}catch(Exception ex){
			String msg= ex.getMessage();
			request.setAttribute("msg",msg); 
			RequestDispatcher dispatcher = request.getRequestDispatcher("mainpage.jsp");
            dispatcher.forward(request, response);
		}
		 
		          
		   
		
		}

}
